RockitJS.Model.Database.Dictionary = function(){

    var $this = this;

    var _init = function()
    { 

    };

    var _resultPage = function($results)
    {
        var $table = new RockitJS.Helper.Table();
        var $count = $results.rows.length;
        var $content = new Array();

        $table.setColumns(new Array("Deutsch", "Tagalog"));


        for (var $i = 0; $i < $count; $i++) {

            var $tmp = new Array();
            $tmp.push($results.rows.item($i).german_word);
            $tmp.push($results.rows.item($i).tagalog_word);
            $content.push($tmp);
            delete $tmp;
        }

        $table.setRows($content);
    };

    var _resultDictionaryList = function($results)
    {

        var $htmlLink = '<li><a href="#" class="dictionary" name="{TABLE}">{LABEL}</a></li>';
        var $nav = "";
        var $count = $results.rows.length;

        for ($i = 0; $i < $count; $i++) {

            var $label = $results.rows.item($i).labelname;
            var $tablename = $results.rows.item($i).tablename;
            var $link = $htmlLink;

            $link = $link.replace(/{TABLE}/g, $tablename);
            $link = $link.replace(/{LABEL}/g, $label);
            $nav += $link;
        }

        $("#dictionaryList").html($nav);

        $(".dictionary").click(
            function() {

                $("html").data("dictionary", $(this).attr("name"));
                
                $("#content").load(
                        "views/dictionary.phtml"
                );
            }
        );
    };

    $this.getPage = function($page)
    {
        this.query(
            "SELECT tagalog_word, german_word FROM german_tagalog LEFT JOIN (german, tagalog) " +
            "ON (german_id = german_tagalog_german_id AND tagalog_id = german_tagalog_tagalog_id) " +
            "WHERE german_word LIKE '" + $page + "%' " +
            "ORDER BY link, german_word ASC", _resultPage);
    };

    $this.getDictionaryList = function()
    {
        $this.query(
            'CREATE TABLE IF NOT EXISTS dictionarylist ' +
            '("dictionarylist_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, ' + 
            '"tablename" VARCHAR NOT NULL UNIQUE, ' +
            '"labelname" VARCHAR NOT NULL UNIQUE)'
        );

        $this.query("SELECT labelname, tablename FROM dictionarylist", _resultDictionaryList);
    };

    $this.createTranslate = function($data)
    {
        var $wordTables;

        $.each($data, function($table, $rows) { 

            if ($table == "dictionarylist") {

                $this.query(
                    'INSERT INTO dictionarylist (tablename, labelname) ' +
                    'VALUES ("' + $rows.de[0] + '", "' + $rows.de[1] + '")'
                );
            }
            else if ($table.indexOf("_")!=-1 && (typeof $wordTables) == "undefined") {

                $wordTables = $table.split("_");

                $this.query('DROP TABLE IF EXISTS ' + $table);
                $this.query(
                    'CREATE TABLE IF NOT EXISTS ' +
                    $table +
                    ' (' + $table + '_' + $wordTables[0] + '_id INTEGER NOT NULL, ' +
                    $table + '_' + $wordTables[1] + '_id INTEGER NOT NULL, ' +
                    'link INTEGER)'
                );

                $.each($rows, function($index, $columns) { 

                    $this.query(
                        'INSERT INTO ' + $table +
                        ' (' + $table + '_' + $wordTables[0] + '_id, ' + $table + '_' + $wordTables[1] + '_id, link) ' +
                        'VALUES ("' + $columns[0] + '", "' +
                        $columns[1] + '", "' + $columns[2] + '")'
                    );
                });

            } else {

                $this.query('DROP TABLE IF EXISTS ' + $table);
                $this.query(
                    'CREATE TABLE IF NOT EXISTS ' +
                    $table +
                    ' ("' + $table + '_id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "' + $table + '_word" VARCHAR NOT NULL  UNIQUE)'
                );

                $.each($rows, function($index, $columns) { 
            
                    $this.query(
                        'INSERT INTO ' + $table +
                        ' (' + $table + '_id, ' + $table + '_word) ' +
                        ' VALUES("' + $columns[0] + '","' + $columns[1] + '")'
                    );
                });
            }
        });
    };


    _init();
}

RockitJS.Model.Database.Dictionary.prototype = new RockitJS.Model.Database.Abstract();